<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .dao{
        width: 800px;
        
       
    }
    .layui-form-item .layui-input-inline{
        float: none;
    }
    </style>
</head>

<body style="padding:8px;">
    <form class="layui-form" lay-filter="cmmForm" action="" onsubmit="return false">
        <div class="layui-form-item dao">
            <label class="layui-form-label" style="width:auto;">演员信息和图片</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-vertype="tips" placeholder="请输入演员姓名" lay-verify="required"
                    autocomplete="off" class="layui-input" id="address">
                <input type="text" name="filmName" lay-vertype="tips" placeholder="请输入饰演角色" lay-verify="required"
                    autocomplete="off" class="layui-input" id="address">
            </div>
            <button type="button" class="layui-btn" id="btnUpload">
                <i class="layui-icon">&#xe67c;</i>图片上传
            </button>
            <input type="text" name="image" id="imges" lay-verify="required" autocomplete="off" style="display: none;">
            <div class="layui-input-inline" style="width:auto;">
                <button class="layui-btn" id="btnSubmit" type="button" lay-submit="">添加</button>
            </div>
        </div>

    </form>
    <table id="courses"></table>

    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script>

        function query(url) {
            const parst = url.split("?");
            if (parst.length == 1) {
                return {};
            }
            const obj = {};
            parst[1].split("&").map(item => item.split("=")).forEach(([p, v]) => {
                obj[p] = decodeURIComponent(v);
            })
            return obj;
        }



        layui.use("upload", function () {
            let upload = layui.upload;

            let uploadInst = upload.render({
                elem: '#btnUpload',
                url: "/api/upload/",
                field: "imgfile",
                acceptMime: "image/*",
                size: 500,
                multiple: true,
                drag: true,
                done: function (res) {
                    $("#imges").val(res.data[0]);
                    console.log(res);
                },
                error: function () {
                    console.log("错误");
                }
            })
        });

        layui.use("form", function () {
            let form = layui.form;
            const courseObj = query(location.href);
            let courseid = courseObj.id;

            form.on('submit', async function (data) {

                await $.post("/api/Movie/actor/" + courseid, data.field);
                layer.msg('添加演员成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    location.href = "./performer.html?id=" + courseid;
                });
            });

        })




        layui.use('table', function () {
            let table = layui.table;
            const courseObj = query(location.href);
            let courseid = courseObj.id;

            table.render({
                elem: '#courses'
                , url: '/api/movie/' + courseid, //数据接口
                page: true //开启分页
                ,
                parseData: function (res) {
                    return {
                        code: 0,
                        message: "",
                        data: res.data.actor,
                        count: res.data.actor.length
                    }
                },
                cols: [
                    [

                        {

                            title: '演员姓名',
                            templet: `<div>{{d.name}}</div>`
                        },
                        {

                            title: '饰演',
                            templet: `<div>{{d.filmName}}</div>`
                        },
                        {

                            title: '图片',
                            templet: "#imgwbd"
                        },
                        {
                            title: "操作",
                            templet: `
                            <div class="layui-btn-group">
                                
                                <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="delStudent('{{d._id}}', this)">
                                    <i class="layui-icon">&#xe640;</i>
                                </button>
                            </div>
                              `
                        }
                    ]
                ],
                done: function (res, curr, count) {
                    hoverOpenImg();//显示大图
                    $('table tr').on('click', function () {
                        $('table tr').css('background', '');
                        $(this).css('background', '<%=PropKit.use("config.properties").get("table_color")%>');
                    });
                }
            });


        });
        function hoverOpenImg() {//显示大图
            var img_show = null;
            $('td img').hover(function () {
                var img = "<img class='img_msg' src='" + $(this).attr('src') + "' style='width:130px;' />";
                img_show = layer.tips(img, this, {
                    tips: [2, 'rgba(41,41,41,.5)']
                    , area: ['160px']
                });
            }, function () {
                layer.close(img_show);
            });
            $('td img').attr('style', 'max-width:90px');
        }


        function delStudent(id, btn) {
            const courseObj = query(location.href);
            let courseid = courseObj.id;

            layer.confirm('确定删除该演员吗？', {
                icon: 0,
                title: '提示'
            }, function (index) {
                $.ajax({
                    url: "/api/Movie/actor/" + courseid
                    , method: "put",
                    data: { key: id }
                });
                $(btn).parents("tr").remove();
                layer.close(index);
            });


        }


    </script>

    <script id="imgwbd" type="text/html">
        <div><img src="{{d.image}}" alt=""></div>
    </script>


</body>

</html>